package halberd.utils;

import halberd.data.Lab;
import halberd.data.Rgb;

public class LabUtils {
	public static void rgb2lab(int r, int g, int b, Lab Lab) {
		Lab.l = (13933 * r + 46871 * g + 4732 * b) >> 16;
		Lab.a = (377 * (14503 * r - 22218 * g + 7714 * b)) >> 24 + 128;
		Lab.b = (160 * (12773 * r + 39695 * g - 52468 * b)) >> 24 + 128;
	}

	public static void lab2rgb(double l, double a, double b, Rgb rgb) {
		int L1 = (int) l;
		int a1 = (int) (a - 128) * 174;
		int b1 = (int) (b - 128) * 410;
		rgb.r = L1 + (a1 * 100922 + b1 * 17790) >> 23;
		rgb.g = L1 - (a1 * 30176 + b1 * 1481) >> 23;
		rgb.b = L1 + (a1 * 1740 - b1 * 37719) >> 23;
	}

}
